package org.csu.agrisupply.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.agrisupply.api.common.CommonResponse;
import org.csu.agrisupply.api.entity.Form;
import org.csu.agrisupply.api.entity.FormOffer;
import org.csu.agrisupply.api.entity.Item;
import org.csu.agrisupply.api.entity.User;
import org.csu.agrisupply.api.persistence.FormMapper;
import org.csu.agrisupply.api.persistence.FormOfferMapper;
import org.csu.agrisupply.api.persistence.ItemMapper;
import org.csu.agrisupply.api.persistence.UserMapper;
import org.csu.agrisupply.api.service.FormOfferService;
import org.csu.agrisupply.api.vo.FormOfferVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("FormOfferService")
public class FormOfferServiceImpl implements FormOfferService {
    @Autowired
    private FormOfferMapper formOfferMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FormMapper formMapper;
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public CommonResponse<List<FormOffer>> getFormOfferList() {
        List<FormOffer> formOfferList = formOfferMapper.selectList(null);
        if(formOfferList.isEmpty()){
            return CommonResponse.createForSuccessMessages("没有分类信息");
        }
        return CommonResponse.createForSuccess(formOfferList);
    }

    @Override
    public CommonResponse<FormOffer> getFormOffer(String formId, String userId) {
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        Form form = formMapper.selectById(formId);
        if(form == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的form");
        }
        QueryWrapper<FormOffer> wrapper = new QueryWrapper<FormOffer>();
        wrapper.eq("userId",userId);
        wrapper.eq("formId",formId);
        FormOffer formOfferresult = formOfferMapper.selectOne(wrapper);
        if(formOfferresult == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的offer");
        }
        return  CommonResponse.createForSuccess(formOfferresult);
    }

    @Override
    public CommonResponse<FormOffer> createOffer(String formId, String userId, float price, int count,String fromAddr){
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        Form form = formMapper.selectById(formId);
        if(form == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的form");
        }
        else if(form.getOffercount() >= 5){
            return CommonResponse.createForSuccessMessages("该form的报价商已满！");
        }
        QueryWrapper<FormOffer> wrapper = new QueryWrapper<FormOffer>();
        wrapper.eq("userId",userId);
        wrapper.eq("formId",formId);
        FormOffer formOfferresult = formOfferMapper.selectOne(wrapper);
        if(formOfferresult != null)
        {
            return CommonResponse.createForSuccessMessages("您已报价，请勿重复报价！如需改价请和采购商议价");
        }
        //生成并插入新的报价单
        FormOffer newformOffer = new FormOffer();
        newformOffer.setFormId(formId);
        newformOffer.setUserId(userId);
        newformOffer.setCount(count);
        newformOffer.setPrice(price);
        newformOffer.setStatus(0);
        newformOffer.setFromAddress(fromAddr);
        formOfferMapper.insert(newformOffer);
        //对应form的offercount加1
        form.setOffercount(form.getOffercount() + 1);
        formMapper.updateById(form);
        return CommonResponse.createForSuccess("报价成功",newformOffer);
    }

    @Override
    public CommonResponse<FormOffer> updateOffer(String formId, String userId, float price, int count, String fromAddr,int status) {
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        Form form = formMapper.selectById(formId);
        if(form == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的form");
        }
        QueryWrapper<FormOffer> wrapper = new QueryWrapper<FormOffer>();
        wrapper.eq("userId",userId);
        wrapper.eq("formId",formId);
        FormOffer formOfferresult = formOfferMapper.selectOne(wrapper);
        if(formOfferresult == null)
        {
            return CommonResponse.createForSuccessMessages("没有该formOffer");
        }
        if(formOfferresult.getStatus() == 2){
            return CommonResponse.createForSuccessMessages("该formOffer已经完成不可再进行修改");
        }
        //更新报价单
        if(count != 0){
            formOfferresult.setCount(count);
        }
        if(price != 0){
            formOfferresult.setPrice(price);
        }
        if (status != 4){
            formOfferresult.setStatus(status);
        }
        formOfferresult.setFormId(formId);
        formOfferresult.setUserId(userId);
        formOfferresult.setFromAddress(fromAddr);
        formOfferMapper.update(formOfferresult,wrapper);
        if(status == 2){
            finishOffer(formOfferresult);
        }
        return CommonResponse.createForSuccess("formOffer修改成功",formOfferresult);
    }

    @Override
    public CommonResponse<List<FormOfferVo>> getFormOfferListbyUserId(String userId) {
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        QueryWrapper<FormOffer> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userId);
        List<FormOffer> formOfferList = formOfferMapper.selectList(wrapper);
        if(formOfferList.isEmpty()){
            return CommonResponse.createForSuccessMessages("没有该ID的offer");
        }
        List<FormOfferVo> formOfferVoList = new ArrayList<>();
        for(FormOffer item : formOfferList){
            FormOfferVo formOfferVo = FormOffertoFormOfferVo(item);
            formOfferVoList.add(formOfferVo);
        }
        return CommonResponse.createForSuccess(formOfferVoList);
    }

    @Override
    public CommonResponse<List<FormOfferVo>> getFormOfferListbyFormId(String formId) {
        Form form  = formMapper.selectById(formId);
        if(form == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的form");
        }
        QueryWrapper<FormOffer> wrapper = new QueryWrapper<>();
        wrapper.eq("formId",formId);
        List<FormOffer> formOfferList = formOfferMapper.selectList(wrapper);
        if(formOfferList.isEmpty()){
            return CommonResponse.createForSuccessMessages("没有该ID的offer");
        }
        List<FormOfferVo> formOfferVoList = new ArrayList<>();
        for(FormOffer item : formOfferList){
            FormOfferVo formOfferVo = FormOffertoFormOfferVo(item);
            formOfferVoList.add(formOfferVo);
        }
        return CommonResponse.createForSuccess(formOfferVoList);
    }

    @Override
    public CommonResponse<FormOfferVo> deleteOffer(String formId, String userId) {
        User user = userMapper.selectById(userId);
        if(user == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的user");
        }
        Form form = formMapper.selectById(formId);
        if(form == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的form");
        }
        QueryWrapper<FormOffer> wrapper = new QueryWrapper<FormOffer>();
        wrapper.eq("userId",userId);
        wrapper.eq("formId",formId);
        FormOffer formOfferresult = formOfferMapper.selectOne(wrapper);
        if(formOfferresult == null)
        {
            return CommonResponse.createForSuccessMessages("没有该ID的offer");
        }
        formOfferMapper.delete(wrapper);
        form.setOffercount(form.getOffercount() - 1);
        return CommonResponse.createForSuccessMessages("删除成功");
    }

    private FormOfferVo FormOffertoFormOfferVo(FormOffer formOffer){
        FormOfferVo formOfferVo = new FormOfferVo();
        formOfferVo.setSaleuserId(formOffer.getUserId());
        formOfferVo.setFormId(formOffer.getFormId());
        formOfferVo.setCount(formOffer.getCount());
        formOfferVo.setPrice(formOffer.getPrice());
        formOfferVo.setOfferstatus(formOffer.getStatus());
        formOfferVo.setFromAddress(formOffer.getFromAddress());
        //得到对应form的属性
        Form form = formMapper.selectById(formOffer.getFormId());
        formOfferVo.setFormstatus(form.getStatus());
        formOfferVo.setUnits(form.getUnits());
        //得到对应user的属性
        User user = userMapper.selectById(formOffer.getUserId());
        formOfferVo.setSalehead(user.getHead());
        formOfferVo.setSaleusername(user.getUsername());
        //得到对应的item属性
        Item item = itemMapper.selectById(form.getItemId());
        formOfferVo.setItemId(item.getItemId());
        formOfferVo.setItemName(item.getItemName());
        formOfferVo.setImage1(item.getImage1());
        return formOfferVo;
    }

    private CommonResponse<Form> finishOffer(FormOffer formOffer){
        Form form = formMapper.selectById(formOffer.getFormId());
        if(form == null){
            return CommonResponse.createForSuccessMessages("没有该ID的form");
        }
        if(form.getCount() > formOffer.getCount()){
            form.setCount(form.getCount() - formOffer.getCount());
            form.setStatus(0);
            formMapper.updateById(form);
        }
        else {
            form.setCount(0);
            form.setStatus(1);
            formMapper.updateById(form);
        }
        return CommonResponse.createForSuccess("成功生成订单",form);
    }
}
